﻿using System;
using System.Collections.Generic;

using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Common.Modularity;

namespace SBPweb.MediaCenter.Modules.Downloader
{

    public abstract class DownloaderServiceBase : ServiceBase, IDownloaderService
    {

        #region Constants

        private static readonly List<string> supportedExtensions = new List<string>(new string[] {
            "torrent"
        });

        #endregion

        #region Private / protected fields

        private string currentExtensionsFilter = supportedExtensions[0];
        protected SortedList<int, IDownloaderFile> downloadList = new SortedList<int, IDownloaderFile>();

        #endregion
        
        #region Constructor logic

        public DownloaderServiceBase(IModule parentModule)
            : base(parentModule, "DownloaderService")
        {
        }

        #endregion

        #region Property accessors

        public virtual string CurrentExtensionsFilter
        {
            get
            {
                return currentExtensionsFilter;
            }
            protected set
            {
                currentExtensionsFilter = value;
            }
        }

        #endregion

        #region Methods

        public abstract IDownloaderFile OpenAndGet(Uri uri);

        public abstract void Start(IDownloaderFile file);

        public abstract void Stop(IDownloaderFile file);

        public abstract void Pause(IDownloaderFile file);

        public virtual void Open(Uri uri)
        {
            OpenAndGet(uri);
        }

        public virtual IList<string> GetSupportedExtensions()
        {
            return supportedExtensions;
        }
        
        public virtual void Start()
        {
            Start(downloadList.Values);
        }

        public virtual void Stop()
        {
            Stop(downloadList.Values);
        }

        public virtual void Pause()
        {
            Pause(downloadList.Values);
        }

        public virtual void Start(IList<IDownloaderFile> files)
        {
            foreach (IDownloaderFile idf in files)
            {
                Start(idf);
            }
        }

        public virtual void Stop(IList<IDownloaderFile> files)
        {
            foreach (IDownloaderFile idf in files)
            {
                Stop(idf);
            }
        }

        public virtual void Pause(IList<IDownloaderFile> files)
        {
            foreach (IDownloaderFile idf in files)
            {
                Pause(idf);
            }
        }

        #endregion
    }

}